#include "certmgrwindow.h"
#include "ui_certmgrwindow.h"

#include "certmanager.h"
#include "certinfowindow.h"

#include <QFileDialog>

#include <openssl/x509.h>


CertMgrWindow::CertMgrWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::CertMgrWindow)
{
  ui->setupUi(this);
  InitCertManager();
  InitUI();

}

CertMgrWindow::~CertMgrWindow()
{
  delete ui;
  cert_manager_->UnInit();
  delete cert_manager_;
}

void CertMgrWindow::InitCertManager()
{
  cert_info_window_ = new CertInfoWindow(0);
  cert_manager_ = new CertManager(this);
  cert_manager_->Init();
  cert_table_model_ = new CertTableModel(this, cert_manager_);
  cert_table_model_->SetSortType(sort_type_);
  ui->certTableView->setModel(cert_table_model_);
  cert_info_window_->hide();
  InitDirectoryTree();
}

void CertMgrWindow::InitUI()
{
  SetupTree();
  SetupAction();
}

void CertMgrWindow::InitDirectoryTree()
{
    QStringList categories(
          std::move(cert_table_model_->GetCategories()));
    for(const auto &c: categories) {
      QTreeWidgetItem* item = new QTreeWidgetItem({c});
      ui->dirTreeWidget->addTopLevelItem(item);
    }
    connect(ui->dirTreeWidget, &QTreeWidget::currentItemChanged,
            [&](QTreeWidgetItem *current, QTreeWidgetItem *previous){
      QString category = current->text(0);
      cert_table_model_->SetCategory(category);
    });
}

void CertMgrWindow::FulfillCertTree()
{
    
}

void CertMgrWindow::ShowOption()
{
    
}

void CertMgrWindow::FindCert(const QString &keyword)
{
    
}

void CertMgrWindow::VerifyCert(const QString &path)
{
    
}

void CertMgrWindow::ExportCert(const QString &path)
{
    
}

void CertMgrWindow::SetupTree()
{
  ui->splitter->setStretchFactor(0, 1);
  ui->splitter->setStretchFactor(1, 3);
}

void CertMgrWindow::SetupAction()
{
  QMenu* file_menu =  ui->menubar->addMenu(tr("file"));
  auto* option_action = file_menu->addAction(tr("file_menu"));
  auto* exit_action = file_menu->addAction(tr("exit"));

  QMenu* operation_menu =  ui->menubar->addMenu(tr("operation"));
  auto* find_action = operation_menu->addAction(tr("find certificate"));
  auto* verify_action = operation_menu->addAction(tr("verify certificate"));
  auto* export_action = operation_menu->addAction(tr("export certificate"));
  auto* import_action = operation_menu->addAction(tr("import certificate"));
  auto* sign_action = operation_menu->addAction(tr("sign file with selected certificate"));

  QMenu* views_menu =  ui->menubar->addMenu(tr("views"));
  auto* position_sort_action = views_menu->addAction(tr("sort by logical position"));
  auto* purpose_sort_action = views_menu->addAction(tr("sort by logic"));
  connect(exit_action, &QAction::triggered, qApp, [&](){
    qApp->exit(0);
  });

  connect(import_action, &QAction::triggered, this, &CertMgrWindow::ImportCertFile);
  connect(ui->certTableView, &QTableView::doubleClicked, [this](const QModelIndex &index){
    X509* cert = cert_table_model_->GetCert(index.row());
    Q_ASSERT(cert);
    cert_info_window_->ShowCert(cert);
  });


}

void CertMgrWindow::ImportCertFile() {
  QString file_path = QFileDialog::getOpenFileName(NULL, tr("Select Certificate Files"),
      QDir::homePath(),
      "PEM files(*.pem *.crt);;Der file(*.der)");
  if(file_path.isEmpty())
    return ;

  cert_manager_->ReadCertFile(file_path.toUtf8());

}

void CertMgrWindow::Sort(SortType type)
{
    
}

void CertMgrWindow::ShowCertWindow(X509 *cert)
{

}

void CertMgrWindow::ShowCert()
{

}
